package com.freeLearning.boot.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freeLearning.boot.common.annotation.Log;
import com.freeLearning.boot.common.enums.LogModuleEnum;
import com.freeLearning.boot.common.result.Result;
import com.freeLearning.boot.core.security.util.SecurityUtils;
import com.freeLearning.boot.system.model.entity.Message;
import com.freeLearning.boot.system.model.query.MessagePageQuery;
import com.freeLearning.boot.system.service.MessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "11.消息通知接口")
@RestController
@RequestMapping("/api/v1/messages")
@RequiredArgsConstructor
public class MessageController {

    private final MessageService messageService;

    @Operation(summary = "分页查询消息通知")
    @PostMapping("/page")
    public Result<IPage<Message>> page(@RequestBody MessagePageQuery messagePageQuery) {
        Page<Message> page = new Page<>(messagePageQuery.getPageNum(), messagePageQuery.getPageSize());
        Long userId = SecurityUtils.getUserId();
        IPage<Message> iPage = messageService.page(page,new LambdaQueryWrapper<Message>()
                .eq(Message::getAcceptId,userId)
                .eq(messagePageQuery.getIsRead()!=null,Message::getIsRead,messagePageQuery.getIsRead())
                .eq(messagePageQuery.getType()!=null,Message::getType,messagePageQuery.getType())
                .like(StrUtil.isNotBlank(messagePageQuery.getTitle()),Message::getTitle,messagePageQuery.getTitle())
                .ge(StrUtil.isNotBlank(messagePageQuery.getPublishTimeStart()),Message::getPublishTime,messagePageQuery.getPublishTimeStart())
                .le(StrUtil.isNotBlank(messagePageQuery.getPublishTimeEnd()),Message::getPublishTime,messagePageQuery.getPublishTimeEnd())
                .orderByDesc(Message::getPublishTime)
        );
        return Result.success(iPage);
    }

    @Operation(summary = "修改消息通知已读状态")
    @PutMapping("/updateIsRead/{id}")
    public Result<Boolean> updateIsRead(@PathVariable Long id) {
        boolean isSuccess = messageService.update(new LambdaUpdateWrapper<Message>()
                .eq(Message::getId,id)
                .set(Message::getIsRead,true));
        return Result.judge(isSuccess);
    }

    @Operation(summary = "全部已读")
    @PutMapping("/updateAllIsRead")
    public Result<Boolean> updateAllIsRead() {
        Long userId = SecurityUtils.getUserId();
        boolean isSuccess = messageService.update(new LambdaUpdateWrapper<Message>()
                .eq(Message::getAcceptId,userId)
                .set(Message::getIsRead,true));
        return Result.judge(isSuccess);
    }

    @Operation(summary = "查询所有未读消息")
    @GetMapping("/queryUnreadMessage")
    public Result<List<Message>> countUnread() {
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<Message> messageLambdaQueryWrapper = new LambdaQueryWrapper<Message>()
                .eq(Message::getAcceptId, userId)
                .eq(Message::getIsRead, false)
                .orderByDesc(Message::getPublishTime);
        Page<Message> page = messageService.page(new Page<>(1, 5), messageLambdaQueryWrapper);
        return Result.success(page.getRecords());
    }

    @Operation(summary = "查询所有未读消息数量")
    @GetMapping("/queryUnreadMessageCount")
    public Result<Long> countUnreadMessage() {
        Long userId = SecurityUtils.getUserId();
        Long count = messageService.count(new LambdaQueryWrapper<Message>()
                .eq(Message::getAcceptId, userId)
                .eq(Message::getIsRead, false));
        return Result.success(count);
    }
}
